package client.comm.baoding.adapter.main

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.text.Layout
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.core.view.setPadding
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import client.comm.baoding.R
import client.comm.baoding.api.bean.TradeIndex
import client.comm.baoding.databinding.ItemJyGoodBinding
import client.comm.baoding.utils.TextTool
import client.comm.commlib.base.BaseLoadmoreAdapter
import client.comm.commlib.base.BindingViewHoder
import client.comm.commlib.dipToPx
import com.bumptech.glide.RequestManager

class JyGoodAdapter : BaseLoadmoreAdapter<TradeIndex.Goods> {

    val manager: RequestManager

    constructor(context: Context, manager: RequestManager) : super(context) {
        this.manager = manager
    }

    override fun createVh(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return BindingViewHoder(
            DataBindingUtil.inflate(
                layoutInflater,
                R.layout.item_jy_good,
                parent,
                false
            )
        )
    }

    override fun bindVh(holder: RecyclerView.ViewHolder, position: Int) {
        val bindingViewHolder = holder as BindingViewHoder<ViewDataBinding>
        val binding = bindingViewHolder.binding as ItemJyGoodBinding
        val bean = getItem(position)
        binding.bean = bean
        binding.event = this

        binding.llLabel.removeAllViews()
        for (e in bean.goods_label) {
            binding.llLabel.addView(getLabel(e))
        }

        manager.load(bean.goods_img).error(R.mipmap.ic_launcher).into(binding.ivGood)
        TextTool.getBuilder("原价：").setAlign(Layout.Alignment.ALIGN_CENTER).setTextSize(14)
            .append("¥").setForegroundColor(Color.parseColor("#E81837")).setTextSize(12)
            .append(bean.price.toString()).setForegroundColor(Color.parseColor("#E81837")).setTextSize(14)
            .into(binding.tvPrice)

        TextTool.getBuilder("特价：").setAlign(Layout.Alignment.ALIGN_CENTER).setTextSize(14)
            .append("¥").setForegroundColor(Color.parseColor("#E81837")).setTextSize(12)
            .append(bean.special_price.toString()).setForegroundColor(Color.parseColor("#E81837"))
            .setTextSize(14)
            .into(binding.tvPriceTj)
    }

    var itemClick: ((bean: TradeIndex.Goods) -> Unit)? = null
    var onBuy: ((bean: TradeIndex.Goods) -> Unit)? = null

    fun itemClick(bean: TradeIndex.Goods) {
        itemClick?.invoke(bean)
    }

    fun onBuy(bean: TradeIndex.Goods) {
        onBuy?.invoke(bean)
    }


    //设置商品label标签
    private fun getLabel(str: String): View? {
        val child = TextView(context)
        child.textSize = 12f
        var drawId = when (str) {
            "热卖" -> R.drawable.label_bg_red1_0
            "新品" -> R.drawable.label_bg_green1_0
            "爱心商品" -> R.drawable.label_bg_blue1_0
            else -> null
        }
        if (drawId != null) {
            child.background = context.getDrawable(drawId)
        }

        child.setTextColor(context.resources.getColor(R.color.white))
        child.setText(str)
        child.setPadding(dipToPx(context.resources, 2f).toInt())
        // 定义LayoutParam
        val params: RelativeLayout.LayoutParams =
            RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        params.rightMargin = dipToPx(context.resources, 10f).toInt()
        child.layoutParams = params
        // 调用带有LayoutParams参数的addView方法
        return child//mContainer.addView(child, params)
    }

}